import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'dart:convert' as convert;

import 'package:flutter/material.dart';

class allpage extends StatefulWidget {
  allpage({Key key, this.partnum}) : super(key: key);
  ValueChanged<int> partnum;
  @override
  State<allpage> createState() => _allpageState();
}

class _allpageState extends State<allpage> {
  Future _future;
  List pageList;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // http://119.91.231.198:3033/getCartoonPartList?cartoon_id=1
    _future = _getcartoonPart();
  }

  _getcartoonPart() async {
    Response response;
    var dio = Dio();
    response = await dio
        .get('http://119.91.231.198:3033/getCartoonPartList?cartoon_id=1');
    var res = convert.jsonDecode(response.toString());
    print(res['result']);
    pageList = res['result'];
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _future,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        print(pageList);
        if (snapshot.connectionState == ConnectionState.done) {
          return Column(children: [
            Container(
              margin: EdgeInsets.only(top: 8, left: 8, bottom: 10),
              alignment: Alignment.centerLeft,
              child: Text(
                '全部章节(' + pageList.length.toString() + ')',
                style: TextStyle(fontSize: 18),
              ),
            ),
            Container(
              child: Wrap(
                children: pageList
                    .asMap()
                    .keys
                    .map((e) => InkWell(
                          onTap: () {
                            widget.partnum(e + 1);
                          },
                          child: Container(
                            alignment: Alignment.center,
                            width: 70,
                            height: 30,
                            // color: Colors.red,
                            decoration: new BoxDecoration(
                              // 设置四周圆角，角度
                              borderRadius:
                                  BorderRadius.all(Radius.circular(4.0)),
                              // 设置四周边框
                              border: new Border.all(
                                  color: Color.fromRGBO(49, 170, 255, 0.7),
                                  width: 2),
                            ),
                            margin: EdgeInsets.all(4),
                            child: Text(
                              (e + 1).toString(),
                              style: TextStyle(fontSize: 18),
                            ),
                          ),
                        ))
                    .toList(),
              ),
            ),
          ]);
        } else {
          return Container();
        }
      },
    );
  }
}
